﻿
//关闭当前页 返回父页面
function closePage(isReload) {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index); //再执行关闭
    if (isReload) {
        parent.search();
    }
}

var _table = null;

layui.use(['form', 'layer'], function () {
    var form = layui.form,
        layer = layui.layer;

    $(function () {
        _table = $('#table');
        loadList();
    });

    function getCheckboxValue() {
        var reValArr = [];
        $("input:checkbox[name=checkbox]:checked").each(function (index, item) {
            if (item.checked) {
                reValArr.push(item.value);
            }
        });
        var reVal = reValArr.toString();
        return reVal;
    }

    //保存
    form.on('submit(save)', function (data) {
        var rightItems = getCheckboxValue();
        app.set('RoleManage.ashx', {
            ActionType: 'UserRight',
            RightItems: rightItems,
            UserGUID: app.getRequest('GUID')
        }, function (data) {
            layer.msg(data.msg);
            if (data && data.success) {
                setTimeout(function () {
                    closePage();
                }, 1000);
            }
        });
        return false;
    });
});

//初始化表格
function loadList() {
    app.list('/Business/MenuManage/MenuManage.ashx', {
        ActionType: 'UserMenuButton',
        UserGUID: app.getRequest('GUID')
    }, function (data) {
        _table.bootstrapTable({
            data: data,
            idField: 'MenuGUID',
            singleSelect: true,
            dataType: 'jsonp',

            // bootstrap-table-treegrid.js 插件配置 -- start
            //在哪一列展开树形

            treeShowField: 'MenuName',
            //指定父id列
            parentIdField: 'ParentId',
            onResetView: function (data) {
                //console.log('load');
                _table.treegrid({
                    //initialState: 'collapsed', // 所有节点都折叠
                    initialState: 'expanded',// 所有节点都展开，默认展开
                    treeColumn: 0,
                    // expanderExpandedClass: 'glyphicon glyphicon-minus',  //图标样式
                    // expanderCollapsedClass: 'glyphicon glyphicon-plus',
                    onChange: function () {
                        _table.bootstrapTable('resetWidth');
                    }
                });

                //只展开树形的第一级节点
                //$table.treegrid('getRootNodes').treegrid('expand');
            }
            // bootstrap-table-treetreegrid.js 插件配置 -- end
        });
    });
}

//格式化页面权限操作
function formatterPagePermission(value, row, index) {
    if (row.PageRight == '1') {
        return '<input id="' + row.MenuGUID + '" style="vertical-align: middle;margin-bottom:2px;" type="checkbox" value="' + row.MenuGUID + '" name="checkbox" checked="checked">';
    } else {
        return '<input id="' + row.MenuGUID + '" style="vertical-align: middle;margin-bottom:2px;" type="checkbox" value="' + row.MenuGUID + '" name="checkbox" >';
    }
}

//格式化按钮权限操作
function formatterButtonPermission(value, row, index) {
    var html = '', mindex = index;
    if (row.ButtonList) {
        var buttonArr = $.parseJSON(row.ButtonList);
        var btnRight = row.ButtonRight;
        if (buttonArr) {
            $.each(buttonArr, function (index, item) {
                if (btnRight && btnRight.indexOf(item.MenuGUID) > -1) {
                    html += '<label style="margin:2px 5px;font-weight: normal;"><input name="checkbox" style="vertical-align: middle;margin-bottom:2px !important;" type="checkbox" id="tb' + item.MenuGUID + '" value="' + item.MenuGUID + '"  checked="checked">' + item.MenuName + '</label>';
                } else {
                    html += '<label style="margin:2px 5px;font-weight: normal;"><input name="checkbox" style="vertical-align: middle;margin-bottom:2px !important;" type="checkbox" id="tb' + item.MenuGUID + '" value="' + item.MenuGUID + '">' + item.MenuName + '</label>';
                }
            });
        }
    }
    return html;
}

//格式化操作按钮
function formatterOperationMenu(value, row, index) {
    var acthtml = '';
    acthtml += '<button type="button" class="layui-btn layui-btn-xs layui-btn-normal" onclick="ckCheckAll(this)">全选</button>';
    acthtml += '<button type="button" class="layui-btn layui-btn-xs layui-btn-danger" onclick="ckNoCheckAll(this)">反选</button>';
    return acthtml;
}

//全选
function ckCheckAll(obj) {
    var rowObj = $(obj).parent().parent();
    $(rowObj).find("input:checkbox[name=checkbox]").each(function (index, item) {
        $(item).prop("checked", true);
    });
}

//反选
function ckNoCheckAll(obj) {
    var rowObj = $(obj).parent().parent();
    $(rowObj).find("input:checkbox[name=checkbox]").each(function (index, item) {
        $(item).prop("checked", false);
    });
}
